package com.kuangstudy.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author renyitong
 * @version 1.0.0
 * @createTime 2023年09月13日
 * @Description
 */
@Controller
@Api(tags = "文章内容处理")
public class ContentController {

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 同步方式 thymeleaf 渲染页面
     * @param contentId
     * @param modelMap
     * @return
     */
    @GetMapping("/content/detail2/{contentId}")
    public String contentDetail2(@PathVariable("contentId") Long contentId, ModelMap modelMap){
        String key = "bbs:content:view:" + contentId;
        Long viewCount = redisTemplate.opsForValue().increment(key);
        // 返回数据和页面
        modelMap.put("viewCount",viewCount);
        return "content/detail2";
    }

    /**
     * 异步处理
     * @param id  文章id
     * @param modelMap
     * @return
     */
    @GetMapping("/content/detail/{id}")
    public String contentDetail(@PathVariable("id") Long id, ModelMap modelMap){
        modelMap.put("contentId",id);
        return "content/detail";
    }

    /**
     * 浏览量 + 1
     * @param contentId
     * @return
     */
    @PostMapping("/content/viewscount/{contentId}")
    @ApiOperation("文章内容浏览量")
    @ResponseBody
    public Long getViewContent(@PathVariable("contentId") Long contentId){
        String key = "bbs:content:view:" + contentId;
        Long viewCount = redisTemplate.opsForValue().increment(key);
        return viewCount;
    }

}
